package draw;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import org.jaaksi.commondemo.R;

/**
 * 创建时间：2018年02月06日14:37 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */

public class TestDrawActivity extends Activity implements DrawView.IDraw {

  private ImageView mIv_bitmap;
  private ImageView mIv_drawable;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_draw);
    final DrawView drawView = (DrawView) findViewById(R.id.drawview);
    mIv_bitmap = (ImageView) findViewById(R.id.iv_bitmap);
    mIv_drawable = (ImageView) findViewById(R.id.iv_drawable);
    drawView.setIDraw(this);
    drawBitmap();
    rotate();

    new Handler().postDelayed(new Runnable() {
      @Override public void run() {
        // {@link #onDraw(android.graphics.Canvas)} will be called at some point in the future.
        drawView.invalidate();
        drawView.setText("world");
        // 最后绘制的文字是world
      }
    }, 1000);
  }

  private void rotate() {
    // 转转90度
    Matrix matrix = new Matrix();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_refresh_progressbar);
    matrix.postRotate(90);
    // 创建新的图片
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    mIv_drawable.setImageBitmap(bitmap);
  }

  private void drawBitmap() {
    //BitmapFactory.Options options = new BitmapFactory.Options();
    //// 只读边不读内容，用于计算宽高，但不载入内存
    //options.inJustDecodeBounds = true;
    //options.inSampleSize
    //// 返回值为null
    //Bitmap xixi =
    //  BitmapFactory.decodeResource(getResources(), R.drawable.ic_refresh_progressbar, options);
    //// 这样获得的宽，高信息是图片的真实尺寸
    //int outWidth = options.outWidth;
    //int outHeight = options.outHeight;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_refresh_progressbar);
    // 载入内存后的bitmap的宽高和图片的真是尺寸可能不同，这与图片所在的目录(xxhdpi)有关系
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int ratio = 2;
    // 创建一个宽高为原bitmap 2倍的bitmap，并创建画布
    Bitmap temp = Bitmap.createBitmap(width * ratio, height * ratio, Bitmap.Config.ARGB_8888);
    // Construct a canvas with the specified bitmap to draw into.
    Canvas canvas = new Canvas(temp); // 创建一个
    // 在画布上绘制bitmap
    Rect rect = new Rect(0, 0, width * ratio, height * ratio);
    canvas.drawBitmap(bitmap, null, rect, null);
    mIv_bitmap.setImageBitmap(temp);
  }

  private void drawDrawable(Canvas canvas) {
    Drawable drawable = getResources().getDrawable(R.drawable.ic_refresh_progressbar);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    drawable.draw(canvas);
  }

  @Override public void onDraw(Canvas canvas) {
    drawDrawable(canvas);
    // 在路径上绘制text
    //canvas.drawTextOnPath();
    //canvas.drawPath();
    // 裁剪路径
    //canvas.clipPath();
    // 裁剪矩形
    //canvas.clipRect();
    //canvas.translate();
    // 错切:将x，y倾斜角度
    //canvas.skew();
    //canvas.save();
    //canvas.saveLayer()
    //canvas.restore();
  }
}
